public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        return dfs(root, 0, targetSum);
    }

    boolean dfs(TreeNode root, int curSum, int target) {
        curSum += root.val;
        if (isLeaf(root)) {
            return curSum == target;
        }
        return root.left != null && dfs(root.left, curSum, target)
                || root.right != null && dfs(root.right, curSum, target);
    }

    private boolean isLeaf(TreeNode root) {
        return root.left == null && root.right == null;
    }
}